var app = getApp();
var WxParse = require('../../lib/wxParse/wxParse.js');
var util = require('../../utils/util.js');
var api = require('../../config/api.js');

Page({
  data: {
    winHeight: "",
    id: 0,
    goods: {},
    gallery: [],
    attribute: [],
    issueList: [],
    comment: [],
    brand: {},
    specificationList: [],
    productList: [],
    relatedGoods: [],
    cartGoodsCount: 0,
    userHasCollect: 0,
    number: 1,
    checkedSpecText: '请选择规格数量',
    openAttr: false,
    noCollectImage: "/static/images/icon_collect.png",
    hasCollectImage: "/static/images/icon_collect_checked.png",
    collectBackImage: "/static/images/icon_collect.png"
  },

  onPullDownRefresh() {
    // 增加下拉刷新数据的功能
    var self = this;
    this.getGoodsInfo();

    wx.stopPullDownRefresh();
  },
  getGoodsInfo: function() {
    let that = this;

    wx.request({
      url: api.GoodsDetail,
      data: {
        goodsID: that.data.id,
      },
      method: 'GET',
      success: function(res) {
        console.log(res);
        if (res.data.status === 1) {
          that.setData({
            goods: res.data.result
          })

          WxParse.wxParse('goodsDetail', 'html', res.data.result.content, that);
        }
      }
    })

  },

  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      id: parseInt(options.goodsID)
      // id: 1181000
    });
    var that = this;
    this.getGoodsInfo();
    // util.request(api.CartGoodsCount).then(function (res) {
    //   if (res.errno === 0) {
    //     that.setData({
    //       cartGoodsCount: res.data.cartTotal.goodsCount
    //     });

    //   }
    // });

    var that = this
    //  高度自适应
    wx.getSystemInfo({
      success: function(res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 100;
        console.log(calc)
        that.setData({
          winHeight: calc
        });
      }
    });

    wx.showShareMenu({
      withShareTicket: true,
      success: function(res) {
        console.log("shareMenu share success");
        console.log("分享" + res);
      },
      fail: function(res) {
        console.log(res);
      }
    })
  },
  onReady: function() {
    // 页面渲染完成

  },
  onShow: function() {
    // 页面显示

  },
  onHide: function() {
    // 页面隐藏

  },
  onUnload: function() {
    // 页面关闭

  },

  onShareAppMessage: function() {
    return {
      title: 'this is a test program mini',
      path: '/pages/goods/goods?goodsID=61',
      success: function(res) {
        console.log(res.withShareTicket[0])

        wx.getShareInfo({
          shareTicket: res.shareTicket[0],
          success: function(res) {
            console.log(res)
          },
          fail: function(res) {
            console.log(res)
          },
          complete: function(res) {
            console.log(res)
          },
        })
      },
      fail: function(res) {
        console.log(res);
      }
    }
  },

  switchAttrPop: function() {
    if (this.data.openAttr == false) {
      this.setData({
        openAttr: !this.data.openAttr,
        collectBackImage: "/static/images/detail_back.png"
      });
    }
  },
  closeAttrOrCollect: function() {
    let that = this;
    if (this.data.openAttr) {
      this.setData({
        openAttr: false,
      });
      if (that.data.userHasCollect == 1) {
        that.setData({
          'collectBackImage': that.data.hasCollectImage
        });
      } else {
        that.setData({
          'collectBackImage': that.data.noCollectImage
        });
      }
    } else {
      //添加或是取消收藏
      util.request(api.CollectAddOrDelete, {
          typeId: 0,
          valueId: this.data.id
        }, "POST")
        .then(function(res) {
          let _res = res;
          if (_res.errno == 0) {
            if (_res.data.type == 'add') {
              that.setData({
                'collectBackImage': that.data.hasCollectImage
              });
            } else {
              that.setData({
                'collectBackImage': that.data.noCollectImage
              });
            }

          } else {
            wx.showToast({
              image: '/static/images/icon_error.png',
              title: _res.errmsg,
              mask: true
            });
          }

        });
    }

  },
  openCartPage: function() {
    wx.switchTab({
      url: '/pages/cart/cart',
    });
  },

  /**
   * 直接购买
   */
  buyGoods: function() {
    var that = this;
    // 直接购买商品
    util.request(api.buyGoods, {
        goodsID: that.data.id,
        username: app.globalData.userInfo.userName
      }, "GET")
      .then(function(res) {
        let _res = res;
        if (_res.status === 1) {
          wx.setStorageSync('orderInfo', res.result);
          wx.navigateTo({
            url: '/pages/pay/pay',
          })
        } else {
          wx.showToast({
            image: '/static/images/icon_error.png',
            title: 'error',
            mask: true
          });
        }

      });
  },
})